<?php
namespace Home\Controller;
use Think\Controller;
use Library\lunar;
class BzcyController extends Controller {
    public function index(){
        indexRvent();//测算首页执行事件
        $this->assign('titleName',titleName('Bzcy',1));//首页名称
        cookie('orderidnum','Bzcy',604800);
        indexRequest($_REQUEST);//测算首页接收数据处理
        $channel=cookie('channel');
        if($channel=='qudao337'){
            //redirect(U("BigLotto/twoInOne",'',false));
        }

        if(cookie('CYloginHC')==''){
            cnzz_pvuv('CY');//执行统计
            cookie('CYloginHC',1,10);
        }
        //渠道获取ID
        $channelID=obtain_channelID('CY');
        $this->assign('channelID',$channelID);

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            getToken();
            getTicket();
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl=wxJsdkData();
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
            wxShareUrl('CY','Bzcy');//分享地址获取

            if(!empty($_REQUEST['code'])){
                cookie('code',$_REQUEST['code'],7100);
            }
            getCode($wzurl);
            getOopenid();
            cookie('uid',cookie('openid'));
        }

        //八字财运随机数
        csRandNum('bzcycs',300000,499999);
        $this->assign("orderidnum",'Bzcy');

        $this->assign('imgPath',cookie('imgPath'));
        $this->assign('yearName',cookie('yearName'));

        if(channelCCA($channel)){
            $this->display("Bzcy/ZM_caiyun_index_CCA");
        }else if(channelVivo($channel)){
            $this->display("Bzcy/ZM_caiyun_index_vivo");
        } else if(channelEdition1($channel)){
            //$this->display("Bzcy/ZM_caiyun_index_edition1");
            //$this->display("Bzcy/ZM_caiyun_index_edition2");
            $this->display("Bzcy/ZM_caiyun_index_edition3");
        }else{
            $this->display("Bzcy/ZM_caiyun_index");
        }
    }
    public function bzcy_zhifu(){
        $this->assign('titleName',titleName('Bzcy',2,'CY'));//首页名称
        $strData=mb_substr(cookie('orderid'),0,2);
        if($strData=='QS')cookie('orderid',null);
        cookie('timeStrto',time()+7200);//倒计时时间戳
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            $this->assign('wxlogin',1);

            if($_SERVER['HTTP_HOST']=='hy.yixueqm.com'){

            }else{
                getToken();
                getTicket();
            }
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket'.cookie('APPID'));
            $wzurl='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
        }
        cookie('orderidnum','Bzcy',604800);

        if($_REQUEST['username']){//返回操作
            cookie('znickname',I('post.username'),604800);
            cookie('zymd',mb_substr($_REQUEST['birthday'],2,10),7200);
            cookie('zhour',mb_substr($_REQUEST['birthday'],13,2),7200);
            cookie('zsex',$_REQUEST['gender'],604800);
            cookie('datetype',mb_substr($_REQUEST['birthday'],0,1));
        }

        if(!empty($_REQUEST['ordernum'])){//未支付订单
            //$arrdata=M()->query("select * from sm_h5test where ordernum='{$_REQUEST['ordernum']}'");
            $arrdata[0]=S('sm_h5test|'.$_REQUEST['ordernum']);
            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd,604800);
            cookie('zhour',$arrdata[0]['hour'],604800);
            cookie('znickname',$arrdata[0]['username'],604800);
            cookie('zsex',$arrdata[0]['sex'],604800);
            cookie('datetype',$arrdata[0]['datetype']);

            cookie('orderid',$_REQUEST['ordernum'],604800);
        }

        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $zyinli=yinli($y,$m,$d);
        cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
        cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

        $data=cookie('orderid');//获取订单号
        if(empty($data)&&empty($_REQUEST['ordernum'])){
            $orderid='CY'.date('Ymd').mt_rand(100,999).date('His');
            cookie('orderid',$orderid,604800);
        }

        //创建订单
        $hour=cookie('zhour');if($hour=='')$hour=0;
        $shop=cookie('orderid');
        $znickname=cookie('znickname');if($znickname=='')$znickname='匿名';
        $zsex=cookie('zsex');if($zsex=='')$zsex=1;
        $channel=cookie('channel');
        $imei=cookie('uid');if(cookie('uid')==''){$imei=cookie('imei');}
        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $datetype=cookie('datetype');if($datetype=='')$datetype=0;
        defaultPrice();

        channelPay($channel,'CY');//查询渠道价格

        if(cookie('couponPrice')){//是否有使用优惠券
            cookie('price',cookie('couponPrice'));
            $this->assign('discount',10);//优惠券价格
        }else{
            $this->assign('discount',0);//优惠券价格
        }

        if(cookie('discountQczh')){//是否有趣测优惠券
            $price=cookie('price');
            cookie('price',$price/2);
            $remark='discountQczh';
        }

        csPay($znickname,$channel);//测试价格判断

        $price=cookie('price');
        if(empty($_REQUEST['ordernum'])){
            $arrdata=S('sm_h5test|'.$shop);
            if(empty($arrdata)){
                $smH5testinter=array(
                    'ordernum'=>$shop, 'price'=>$price, 'username'=>$znickname, 'typeid'=>18, 'sex'=>$zsex, 'createtime'=>date('Y-m-d H:i:m'),
                    'status'=>-1, 'datetype'=>$datetype, 'year'=>$y, 'month'=>$m, 'day'=>$d, 'hour'=>$hour, 'date_remark'=>$remark,
                    'appuserid'=>$imei, 'ip'=>$_SERVER['REMOTE_ADDR'], 'channel'=>$channel, 'auto_send_sms'=>0, 'place'=>0,
                );
                S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                cacheZhiFuOrderid($shop);//支付页缓存订单操作

                $IndexController=A('Index');//Index控制器
                $IndexController->subNumber('CY');//提交订单次数统计
            }else if(!cookie($_REQUEST['username'])){//返回操作
                //更新订单
                $arrdata['username']=$znickname;$arrdata['datetype']=$datetype;
                $arrdata['year']=$y;$arrdata['month']=$m;$arrdata['day']=$d;$arrdata['sex']=$zsex;
                S('sm_h5test|'.$shop,$arrdata,2592000);//2592000为30天
            }
        }

        if(cookie('zsex')==1){$zsex='男';}else{$zsex='女';}
        zwmp(cookie('zymd'), cookie('zhour'),$zsex,'sizhu');
        $sizhu=I('globals.sizhu');
        $this->assign('sizhu',$sizhu);//Z四柱

        if($datetype==1){
            $this->assign('datetype',"农历 ".cookie('zyinli')." ".mb_substr($sizhu[3],1,1,'utf-8')."时");
        }else{
            $this->assign('datetype',"阳历 ".cookie('zyangli'));
        }


        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            if (strpos($user_agent, 'Windows') == true||strpos($user_agent,'Mac') == true) {
                //Windows 电脑访问
                if(channelSaoma($channel)){
                    $this->assign('pc',1);
                    $saomaUrl=wxsaoma('CY','八字财运',cookie('orderid'));//微信扫码回调参数
                    $this->assign('wxsaoma',$saomaUrl);
                }
            }
        }else{
            $xml=wxgzhzf('CY','八字财运',cookie('orderid'));//公众号支付回调参数
            $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $val = json_decode(json_encode($xmlstring),true);
            $noncestr=noncestr(15);//随机字符串
            $timestamp=time();//时间戳

            $stringA="appId=".cookie('APPID')."&nonceStr={$noncestr}&package=prepay_id={$val['prepay_id']}&signType=MD5&timeStamp={$timestamp}";
            $stringSignTemp=$stringA."&key=".cookie('wxKEY');
            $sign=strtoupper(md5($stringSignTemp));

            $this->assign('timeStamp',$timestamp);
            $this->assign('nonceStr',$noncestr);
            $this->assign('package','prepay_id='.$val['prepay_id']);
            $this->assign('signType','MD5');
            $this->assign('paySign',$sign);
            $this->assign('appId',cookie('APPID'));
        }

        $this->assign('csNameJump',U('Bzcy/jieguoye','',false).'?ordernum='.cookie('orderid'));//支付成功跳转地址

        $this->assign('channel',cookie('channel'));
        $this->assign('znickname',cookie('znickname'));
        $this->assign('sex',cookie('zsex'));

        $dataYearType=cookie('dataYearType');//2021标识
        if(empty($dataYearType)){$dataYearType=2020;}
        $this->assign('yearName',$dataYearType);

        if(channeFree($channel)){
            $this->display("Bzcy/ZM_caiyun_zhifu_free");
        }else if(channelCCA($channel)){
            $this->display("Bzcy/ZM_caiyun_zhifu_CCA");
        }else if(channelVivo($channel)){
            $this->display("Bzcy/ZM_caiyun_zhifu_vivo");
        } else if(channelEdition1($channel)){
            $this->display("Bzcy/ZM_caiyun_zhifu_edition1");
        } else{
            $this->display("Bzcy/ZM_caiyun_zhifu");
        }
    }
    public function jieguoye(){
        jieguoInterface();//结果页公共函数
        $this->assign('titleName',titleName('Bzcy',3));//首页名称
        $channel=cookie('channel');
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            $this->assign('wxlogin',1);

            if($_SERVER['HTTP_HOST']==cookie('jieguoUrlYM')){

            }else{
                getToken();
                getTicket();
            }
            $noncestr=noncestr(15);
            $timestamp=time();
            $ticket= S('ticket');
            $wzurl='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

            $this->assign('appid',cookie('APPID'));
            $this->assign('timestamp',$timestamp);
            $this->assign('noncestr',$noncestr);
            $this->assign('signature',$signature);
        }

        if(empty($_REQUEST['ordernum'])){
            $y=mb_substr(cookie('zymd'),0,4);//年
            $m=mb_substr(cookie('zymd'),5,2); //月
            $d=mb_substr(cookie('zymd'),8,2);//日
            $zyinli=yinli($y,$m,$d);
            cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
            cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

            $zymd= cookie('zymd');
        }else{
            $ordernum=$_REQUEST['ordernum'];
            $arrdata[0]=jieguoyeArrData($ordernum);
            $phone=$arrdata[0]['phone'];$this->assign('phone',$phone);
            $email=$arrdata[0]['email'];$this->assign('email',$email);
           // if($channel=='qudao100'){
                $this->assign('phonebindNum',1);//手机绑定弹窗
                $wzurl=wxJsdkData();
                $this->assign('jgOrderUrl',$wzurl);

//                $arrdata=S('sm_h5test|'.$ordernum);
//                $arrdata['phone']='';
//                $arrdata['email']='';
//                S('sm_h5test|'.$ordernum,$arrdata,2592000);//2592000为30天
           // }


            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd,604800);
            cookie('zhour',$arrdata[0]['hour'],604800);
            cookie('znickname',$arrdata[0]['username'],604800);
            cookie('zsex',$arrdata[0]['sex'],604800);

            $y=mb_substr(cookie('zymd'),0,4);//年
            $m=mb_substr(cookie('zymd'),5,2); //月
            $d=mb_substr(cookie('zymd'),8,2);//日
            $zyinli=yinli($y,$m,$d);
            cookie('zyangli',$y.'年'.$m.'月'.$d.'日'.' '.cookie('zhour').'时',604800);//阳历
            cookie('zyinli',$zyinli[0].'年'.$zyinli[1].$zyinli[2],604800);//阴历

            $zymd= cookie('zymd');
            cookie('orderid',$arrdata[0]['ordernum'],604800);
        }

        if($channel=='sjg'){
            $wzurl=wxJsdkData();
            $loopNum=loopSecond(cookie('orderid'));//循环读秒
            if($loopNum<120){
                $this->assign('wzurl',$wzurl);
                $this->display('Index/wait-loader');exit;
            }
        }

        //一对一亲算下单
        $QSorder=new \Library\toolOrder();
        $wxzhifuArr=$QSorder->QSorderXD();
        if($wxzhifuArr==1){
            $this->assign('qsPay',1);
        }else{//更新数据hu
            if (strpos($user_agent, 'MicroMessenger') == false) {
                //非微信浏览器访问
            }else{
                $this->assign('timeStamp',$wxzhifuArr['timeStamp']);
                $this->assign('nonceStr',$wxzhifuArr['nonceStr']);
                $this->assign('package',$wxzhifuArr['package']);
                $this->assign('signType',$wxzhifuArr['signType']);
                $this->assign('paySign',$wxzhifuArr['paySign']);
                $this->assign('appId',$wxzhifuArr['appId']);
            }
        }

        if(cookie('zsex')==1){
            $zsex='男';
        }else{
            $zsex='女';
        }
        if($zymd!=''){
            zwmp($zymd,cookie('zhour'),$zsex,'zw');
            $this->assign('shengxiao',I('globals.shengxiao'));//生肖
            $this->assign('sizhu',I('globals.sizhu'));//Z四柱
            $ssarray=array();
            array_push($ssarray,implode(I('globals.nianzhuss')));
            array_push($ssarray,implode(I('globals.yuezhuss')));
            array_push($ssarray,implode(I('globals.shizhuss')));
            $this->assign('nianzhuss',$ssarray);//十神
            $this->assign('canggan',I('globals.canggan'));//藏干

            $this->assign('zhishen',I('globals.zhishen'));//支神
            $this->assign('sizhuny',I('globals.sizhuny'));//四柱纳音
            $this->assign('bzdishi',I('globals.bzdishi'));//八字地势
            $this->assign('wxsqs',I('globals.wxsqs'));//旺相休囚死
            $this->assign('xiys',I('globals.xiys'));//喜用神
            $this->assign('taiyuan',I('globals.taiyuan'));//胎元
            $this->assign('rikong',I('globals.rikong'));//日空
            $this->assign('dayun',I('globals.dayun'));//大运
            $this->assign('dayunx',I('globals.dayunx'));//大运x
            $this->assign('dayuny',I('globals.dayuny'));//大运y
            $this->assign('dayungz',I('globals.dayungz'));//大运干支
            $this->assign('dayunss',I('globals.dayunss'));//大运十神
            lnyuncheng($zymd,I('globals.dayun'),I('globals.dayunss'));
            $this->assign('lnyuncheng',I('globals.lnyuncheng'));//流年运程
            $orderid=cookie('orderid');
            $createtime=M()->query("select createtime from sm_h5test where ordernum='{$orderid}'");
            $this->assign('createtime',$createtime[0]['createtime']);//创建时间
            fortune(I('globals.shengxiao'),I('globals.bzrizhu'));
            $this->assign('fortune',I('globals.fortune'));//流年财运
            syfz(I('globals.srg'),I('globals.arrzx'),I('globals.arrtf'),I('globals.arrjix'),I('globals.arrxiongx'),I('globals.shua'));
            $this->assign('syarrzhux',I('globals.syarrzhux'));//事业主星状况
            bzcaiyun(I('globals.sizhu'),$zymd);
            $this->assign('arrcyd',I('globals.arrcyd'));//八字财运日
            $this->assign('arrcyy',I('globals.arrcyy'));//八字财运年
            $this->assign('zpcai',I('globals.zpcai'));//八字财正偏财随机数
            $this->assign('wangyun',I('globals.wangyun'));//八字财运旺运随机数
            $this->assign('arrzygua',I('globals.arrzygua'));//八字财运64卦
//            dump(I('globals.syarrzhux'));
//            exit;
        }

        $this->assign('channel',cookie('channel'));
        $this->assign("orderidnum",'Bzcy');
        $this->assign('sex',cookie('zsex'));

        if(channelCCA($channel)){
            $this->display("Bzcy/ZM_caiyun_jieguoye_CCA");
        }else if(channelVivo($channel)){
            $this->display("Bzcy/ZM_caiyun_jieguoye_vivo");
        } else{
            $this->display("Bzcy/ZM_caiyun_jieguoye");
        }
    }

    public function payzfEnd(){
        $this->assign('orderName','CY');
        $this->display('Index/payzfEnd');
    }

    public function pay(){//支付宝支付
        $price=cookie('price');
        $orderid=cookie('orderid');
        $this->assign('ordername1','八字财运');
        $this->assign('ordername2','知命-八字财运');

        $this->display("Index/pay");
//        header("location:http://120.24.82.134/api/wappay/paybzcy.php?orderid=".cookie('orderid')."&price={$price}");
    }
    public function payZfb(){
        $this->assign('ordername1','大师一对一亲算');
        $this->assign('ordername2','知命-大师一对一亲算');
        $this->display("Index/pay");
    }

    public function zzhifu(){
        $data=M()->query("select status from sm_h5test where ordernum='".cookie('orderid')."'");
        $this->assign('status',$data[0]['status']);
        $this->display("Index/ZM_zzhifu");
    }
    public function paywx(){//H5微信支付
        H5ZMwxzhifu('CY','八字财运',cookie('orderid'),'Bzcy');//知命
    }

    public function CountDown(){//倒计时
        $timeStrto=cookie('timeStrto');
        if(empty($timeStrto)){
            $timeStrto=time()+7200;
            cookie('timeStrto',$timeStrto);//倒计时时间戳
        }
        $strto=$timeStrto-time();
        $h=intval($strto/60/60);
        $i=intval(($strto-60*60*$h)/60);
        $s=($strto-60*60*$h)%60;

        $h=str_pad($h,2,'0',STR_PAD_LEFT);
        $i=str_pad($i,2,'0',STR_PAD_LEFT);
        $s=str_pad($s,2,'0',STR_PAD_LEFT);

        if($strto>0){
            $data="{$h}:{$i}:{$s}";
            echo json_encode($data);
        }else{
            $data="00:00:00";
            echo json_encode($data);
        }
    }

    public function cs(){
        set_time_limit(1200);
        exit;
        //16430
        for($i=16201;$i<=16430;$i++){
            $url="http://www.qqq681.cn/mO{$i}.html";
            $json=curlGet($url);
            preg_match("/id=\"right\"[\s\S]*mt10/",$json,$imgStr);
            preg_match("/UpImg[\s\S]*.jpg/",$imgStr[0],$imgStr);
            if($imgStr[0]){
                $imgUrl="http://www.qqq681.cn/{$imgStr[0]}";
            }else{
                $imgUrl="";
            }

            $data=str_replace(" ",'',$json);
            $data=strip_tags($data);
            $data = str_replace(array("&nbsp;", "&ensp;", "&emsp;","&thinsp;","&zwnj;","&zwj;","&ldquo;","&rdquo;"), "",  $data);
            $Arr=array_values(array_filter(explode("\r\n",$data)));
            $titme=$Arr[8];
            $name=$Arr[9];
            foreach($Arr as $key=>$value){
                if($key==10){
                    $value=str_replace('　','',$value);
                    $value=str_replace("{$name}{$name}",$name,$value);
                    $text=$value;
                }else if($key>10){
                    $value=str_replace('　','',$value);
                    $strCount=substr_count($value,'分享');
                    if(!$strCount){
                        if($value){
                            $text.="|{$value}";
                        }
                    }else{
                        break;
                    }
                }else if($key>15){
                    break;
                }
            }

            if(isset($text)){
                //M()->query("insert into data_other_gjmr (name,imgUrl,title,text)values('{$name}','{$imgUrl}','{$titme}','{$text}')");
            }
        }
        echo $i;
    }

}